package com.example.bishe.controller.jg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.example.bishe.entity.*;
import com.example.bishe.service.*;
import com.example.bishe.service.impl.*;
import com.example.bishe.toolclass.AllBmList;
import com.example.bishe.toolclass.CUD;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yws
 * @Date 2022/4/25 15:14
 */
@RestController
@RequestMapping("/jg")
public class JgController {
    @Resource
    Bzks_bjService bj = new Bzks_bjServiceImpl();
    @Resource
    Bzks_zyService zy = new Bzks_zyServiceImpl();
    @Resource
    Bzks_yxService yx = new Bzks_yxServiceImpl();
    @Resource
    JxbmService jxbmService = new JxbmServiceImpl();
    @Resource
    Zxbz_dwService dw = new Zxbz_dwServiceImpl();

    @PostMapping("/bj")
    public String  bj(){
        Map<String, String> map = new HashMap<>();
        Bzks_bj bzks_bj = new Bzks_bj();
        Bzks_bj[] bzks_bjs = bj.select_bj(bzks_bj);
        for (Bzks_bj bj:bzks_bjs) {
            map.put(bj.getBh(),bj.getBjmc());
            System.out.println(bj.getBjmc());
        }
        String s = JSON.toJSONString(map);
        return s;
    }

    @PostMapping("/zy")
    public String  zy(){
        Map<String, String> map = new HashMap<>();
        Bzks_zy[] bzks_zies = zy.select_zy(new Bzks_zy());
        for (Bzks_zy zy:bzks_zies) {
            map.put(zy.getDm(),zy.getMc());
        }
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @PostMapping("/yx")
    public String yx(){
        Map<String, String> map = new HashMap<>();
        Bzks_yx[] bzks_yxes = yx.select_yx(new Bzks_yx());
        for (Bzks_yx yx_ :
                bzks_yxes) {
            map.put(yx_.getDm(),yx_.getMc());
        }
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @PostMapping("/jxbm")
    public String jxbm(){
        Map<String, String> map = new HashMap<>();
        Jxbm[] jxbms = jxbmService.selectJxbm(new Jxbm());
        for (Jxbm jxb :
                jxbms) {
            map.put(jxb.getDm(),jxb.getBmname());
        }
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @PostMapping("/dw")
    public String dw(){
        Map<String, String> map = new HashMap<>();
        Zxbz_dw[] zxbz_dws = dw.select_dw(new Zxbz_dw());
        for (Zxbz_dw zxbz_dw :
                zxbz_dws) {
            map.put(zxbz_dw.getDm(), zxbz_dw.getMc());
        }
        String s = JSON.toJSONString(map);
        System.out.println(s);
        return s;
    }
    @Resource
    CUD cud;
    @RequestMapping("/selectAllJg")
    public String selectAllJg(){
        HashMap map = cud.selectAllJg();
        JSONArray params = JSONArray.parseArray(map.get("department")+"");
        System.out.println(params.getJSONObject(0).get("name")+(params.size()+""));
        Map<String, AllBmList> departments = new HashMap();
        for (int i = 0; i < params.size(); i++) {
            //先创建所有的部门信息
            AllBmList allBmList = new AllBmList();
            allBmList.setId((Integer) params.getJSONObject(i).get("id"));
            allBmList.setName((String) params.getJSONObject(i).get("name"));
            allBmList.setParentid((Integer) params.getJSONObject(i).get("parentid"));
            departments.put(params.getJSONObject(i).get("id")+"",allBmList);
        }
        for (int i = 0; i < params.size(); i++) {
            //构建部门信息
            if(!(params.getJSONObject(i).get("parentid")+"").equals("0")){
                AllBmList allBmList = departments.get(params.getJSONObject(i).get("id")+"");//本部门
                //System.out.println(allBmList.toString());
                AllBmList allBmListfather = departments.get(params.getJSONObject(i).get("parentid")+"");//父部门
                //System.out.println(allBmListfather.toString());
                //List<AllBmList> list = allBmListfather.getList();
                AllBmList[] list = allBmListfather.getList();
                //list.add(allBmList);
                AllBmList[] lists = new AllBmList[list.length+1];
                for (int j = 0; j < list.length; j++) {
                    lists[j]=list[j];
                }
                int x = list.length;
                lists[x]=allBmList;
                allBmListfather.setList(lists);
                departments.put(allBmListfather.getId()+"",allBmListfather);
            }
        }
        String s = JSON.toJSONString(departments.get("1"));
        return s;
    }
}
